package com.blue.rpc.core.invoke;

import com.blue.rpc.core.annotation.RpcParser;
import com.blue.rpc.core.message.RpcRequest;
import com.blue.rpc.core.message.RpcResponse;
import com.blue.rpc.util.RpcServerUtil;

/**
 * RPC方法调用
 * 
 * @author zhengj
 * @since 1.0 2017年3月13日
 */
public class RpcInvoker
{
	private RpcParser parser;
	
	public RpcInvoker(RpcParser parser)
	{
		this.parser = parser;
	}
	
	public RpcResponse invoke(RpcRequest request)
	{
		long start = System.currentTimeMillis();
		RpcResponse response = RpcServerUtil.create(request);
		try
		{
			Object result = parser.invoke(request.getService(), request.getMethod(), request.getParams());
			response.setResult(result);
			RpcServerUtil.log(request, response, start);
		}
		catch (Exception e)
		{
			response.setException(e);
			RpcServerUtil.log(request, response, start);
		}
		return response;
	}
	
}
